﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using AjaxControlToolkit;
using System.Web.Script.Services;
using HySoft.Cash.Business;
using HySoft.Cash.Structure;
using System.Web.UI;
using System.Data.SqlClient;
using System.Configuration;

namespace PayRoller.WebServices
{
    /// <summary>
    /// Organization 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [ScriptService]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class Organization : System.Web.Services.WebService
    {
        /// <summary>
        /// 给自动完成的方法获取所有符合条件的企业名称
        /// </summary>
        /// <param name="prefixText"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        [WebMethod]
        [ScriptMethod]
        public string[] GetOrganizations(string prefixText, int count)
        {
            if (count == 0)
            {
                count = 50;
            }

            List<string> items = new List<string>();
            string connString = ConfigurationManager.AppSettings["SQLConnString"];
            using (SqlConnection conn = new SqlConnection(connString))
            {
                string commandText = string.Format( "select sid,orgName from tOrgInfo where orgName like '{0}%' ",prefixText);
                conn.Open();
                using (SqlCommand comm = new SqlCommand(commandText, conn))
                {
                    using (SqlDataReader reader = comm.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            string tempItem = string.Format("{0}[{1}]", Convert.ToString(reader["orgName"]), Convert.ToString(reader["sid"]));
                            items.Add(tempItem);
                        }
                    }
                }
            }

            return items.ToArray();
        }
    }
}
